﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PartyManagement.Models
{
    public class FoodController : Controller
    {
        private PartyManagementContext db = new PartyManagementContext();

        //
        // GET: /Food/

        public ActionResult Index()
        {
            var foods = db.Foods.Include(f => f.FoodCategory);
            return View(foods.ToList());
        }

        //
        // GET: /Food/Details/5

        public ActionResult Details(int id = 0)
        {
            Food food = db.Foods.Find(id);
            if (food == null)
            {
                return HttpNotFound();
            }
            return View(food);
        }

        //
        // GET: /Food/Create

        public ActionResult Create()
        {
            ViewBag.foodCategoryID = new SelectList(db.FoodCategories, "foodCategoryID", "foodCategoryName");
            return View();
        }

        //
        // POST: /Food/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Food food)
        {
            if (ModelState.IsValid)
            {
                db.Foods.Add(food);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.foodCategoryID = new SelectList(db.FoodCategories, "foodCategoryID", "foodCategoryName", food.foodCategoryID);
            return View(food);
        }

        //
        // GET: /Food/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Food food = db.Foods.Find(id);
            if (food == null)
            {
                return HttpNotFound();
            }
            ViewBag.foodCategoryID = new SelectList(db.FoodCategories, "foodCategoryID", "foodCategoryName", food.foodCategoryID);
            return View(food);
        }

        //
        // POST: /Food/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Food food)
        {
            if (ModelState.IsValid)
            {
                db.Entry(food).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.foodCategoryID = new SelectList(db.FoodCategories, "foodCategoryID", "foodCategoryName", food.foodCategoryID);
            return View(food);
        }

        //
        // GET: /Food/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Food food = db.Foods.Find(id);
            if (food == null)
            {
                return HttpNotFound();
            }
            return View(food);
        }

        //
        // POST: /Food/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Food food = db.Foods.Find(id);
            db.Foods.Remove(food);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        [ChildActionOnly]
        public ActionResult _FoodCategoryPartial()
        {
            return PartialView(db.FoodCategories.ToList());
        }

        //
        // GET: /GetFoodList/id

        public ActionResult GetFoodList(int? id)
        {
            int eventId = Convert.ToInt32(Session["event_id"]);
            Menu menu = db.Menus.SingleOrDefault(p => p.eventID == eventId);
            int menuId = menu.menuID;
            List<FoodInMenu> foodInMenus = db.FoodInMenus.Where(p => p.menuID == menuId).ToList();

            List<Food> foods;
            if (!id.HasValue || id == 0)
            {
                foods = db.Foods.Where(p => !p.FoodInMenus.Any(q => q.foodID == p.foodID)).
                    Where(p => p.isAvailable == true).ToList();
            }
            else
            {
                foods = db.Foods.Where(p => !p.FoodInMenus.Any(q => q.foodID == p.foodID)).
                    Where(p => p.foodCategoryID == id).Where(p => p.isAvailable == true).ToList();
            }
            return View(foods);
        }

        [HttpPost]
        public ActionResult GetFoodList(int[] chkId)
        {
            int eventId = Convert.ToInt32(Session["event_id"]);
            Menu menu = db.Menus.SingleOrDefault(p => p.eventID == eventId);
            int menuId = menu.menuID;
            foreach (var i in chkId)
            {
                FoodInMenu foodInMenu = new FoodInMenu();
                foodInMenu.foodID = i;
                foodInMenu.menuID = menuId;
                foodInMenu.quantity = 1;
                db.FoodInMenus.Add(foodInMenu);

                Food food = db.Foods.SingleOrDefault(p => p.foodID == foodInMenu.foodID);
                food.quantity -= 1;

                db.SaveChanges();
            }

            return RedirectToAction("GetMenuForEvent", "Menu", new { eventID = Session["event_id"] });
        }
    }
}